package leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * description:
 * author:张腾
 * date:2021-07-15
 */
public class leetcode144_2 {
    public List<Integer> preorderTraversal(TreeNode head) {
        if (head==null) return null;

        LinkedList<TreeNode> stack = new LinkedList<>();
        ArrayList<Integer> res = new ArrayList<>();
        stack.push(head);
        while (!stack.isEmpty()){
            TreeNode node = stack.pop();
            res.add(node.val);
            if (node.right!=null) stack.push(node.right);
            if (node.left!=null) stack.push(node.left);
        }

        return res;
    }
}
